package net.knightwatch.kwtools.database;

import net.knightwatch.kwtools.walkthrough.items.TextAnswer;
import android.content.ContentValues;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TextQuestionTable {
	private static final String TAG = "TextQuestionTable";
	// Database Table
	public static final String TABLE_TEXTQUESTIONS = "TextQuestions";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_QUESTION_ID = "questionId";
	public static final String COLUMN_HINT_TEXT = "hintText";
	public static final String COLUMN_VALUE_TYPE = "valueType";
	
	// Database Creation SQL Statement
	private static final String DATABASE_CREATE = "CREATE TABLE " 
			+ TABLE_TEXTQUESTIONS
			+ "("
			+ COLUMN_ID + " integer primary key autoincrement, "
			+ COLUMN_QUESTION_ID + " integer not null, "
			+ COLUMN_HINT_TEXT + " text, "
			+ COLUMN_VALUE_TYPE + " integer not null"
			+ ");";
	
	
	public static void onCreate(SQLiteDatabase database) {
		Log.i(TAG, "Creating " + TABLE_TEXTQUESTIONS + " Table");
		try {
			database.execSQL(DATABASE_CREATE);
			// Create the default questions
		} catch (Exception e){
			Log.v(TAG, e.toString());
		} finally {
			Log.d(TAG, "Table " + TABLE_TEXTQUESTIONS + " has been created.");
		}
	}
	public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion){
		if(oldVersion < 13){
			// Upgraded databased to change which keyboard is dispayed on the text type question on 9/30/2012 issue # 32
			database.beginTransaction();
			try{
				// Alter the database and add the new columnn.
				String query = "ALTER TABLE "
						+ TABLE_TEXTQUESTIONS
						+ " ADD "
						+ COLUMN_VALUE_TYPE
						+ " integer not null DEFAULT "
						+ android.text.InputType.TYPE_CLASS_TEXT
						+ ";";
				Log.d(TAG, query);
				database.execSQL(query);
				Log.d(TAG, "Upgraded TextQuestionsTable");
				// Update the records in the table and set to a default value of "TEXT"
				database.setTransactionSuccessful();
			} catch (SQLException e){
				e.printStackTrace();
			} finally {
				database.endTransaction();
			}
		}
	}
}
